﻿using System;
using System.Web;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace TaiLieu
{
    public class Config
    {
        public static string SteupColorForString(string str,string color)
        {
            return "<span  style=\"color:"+color+"\">(chỉ đc nhập 100 kí tự)</span>";
        }
        // cat lay tin
        public static string Cut_String(string s)
        {
            string content = s;
            try
            {
                if (content.Length > 200)
                {
                    content = content.Substring(0, 200);
                    content = content.Substring(0, content.LastIndexOf(" ")) + ".";
                }
            }
            catch { }
            return content;
        }
        public static string Cut_String(string s, int munberChar)
        {
            string content = s;
            try
            {
                if (content.Length > munberChar)
                {
                    content = content.Substring(0, munberChar);
                    content = content.Substring(0, content.LastIndexOf(" ")) + ".";
                }
            }
            catch { }
            return content;
        }
        public static string subString(string str, int start, int end)
        {
            try
            {
                return str.Substring(start, end - start - 1);
            }
            catch
            {
                return "";
            }
        }
        public static string GetStrFromWebConfig(string key)
        {
            try
            {
                return ConfigurationManager.AppSettings[key].ToString();
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        ///  public void SetRadionButon(ref RadioButtonList rdbtn, string name)
        /// </summary>
        /// <param name="rdbtn"></param>
        /// <param name="name"></param>
        public static void SetRadionButon(ref RadioButtonList rdbtn, string name)
        {
            foreach (ListItem item in rdbtn.Items)
            {
                if (item.Text == name)
                {
                    item.Selected = true;
                    return;
                }
            }
        }
        /// <summary>
        ///  public static void SetDropDownList(ref DropDownList rdbtn, string name)
        /// </summary>
        /// <param name="rdbtn"></param>
        /// <param name="name"></param>
        public static void SetDropDownList(ref DropDownList rdbtn, string name)
        {
            foreach (ListItem item in rdbtn.Items)
            {
                if (item.Text == name)
                {
                    item.Selected = true;
                    return;
                }
            }
        }
        /// <summary>
        /// 26-06-2011
        /// Test chuỗi nhập vào có đúng định dạng mail ko?
        /// </summary>
        /// <param name="EmailAddress"></param>
        /// <returns></returns>
        public static bool CheckFormatEmail(string EmailAddress)
        {
            string strPattern = "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
            if (System.Text.RegularExpressions.Regex.IsMatch(EmailAddress, strPattern))
            { return true; }
            return false;
        }
        /// <summary>
        /// 26-06-2011
        /// Test chuỗi nhập vào có là chuỗi số ko?
        /// </summary>
        /// <param name="pText"></param>
        /// <returns></returns>
        public static bool CheckDigitNumber(string pText)
        {
            Regex regex = new Regex(@"^[-+]?[0-9]*\.?[0-9]+$");
            return regex.IsMatch(pText);
        }
        public static string ByLanguage(string lang, string vnString, string elString)
        {
            if (lang.CompareTo(QueryString._vn) == 0)
            {
                return vnString;
            }
            return elString;
        }
        //function insert " "
        public static string InsertBackSpace(string str)
        {
            if (str.Length > 15 && str.Contains(" ") == false)
            {
                int leng = str.Length;
                int tam = 0;
                string tam1 = "";
                string tam2 = "";
                do
                {
                    tam1 = str.Substring(0, tam1.Length + 15);
                    tam2 = str.Substring(tam1.Length, leng - tam1.Length);
                    str = tam1 + " " + tam2; ;
                    tam += 15;
                } while (tam <= str.Length - 15);
            }
            return str;
        }
        public static string LimitLenghtString(string str)
        {
            if (str.Length >= 15)
            {
                if (str.Length > 15)
                    return InsertBackSpace(str).Substring(0, 15) + "...";
                else
                    return InsertBackSpace(str);
            }
            return str;
        }
        //
        public static bool UseAjax()
        {
            try
            {
                return Convert.ToBoolean(ConfigurationManager.AppSettings["UseAjax"]);
            }
            catch
            {
                return false;
            }
            return false;
        }
        public static string GetValue(string tagName)
        {
            return Convert.ToString(ConfigurationManager.AppSettings[tagName]);
        }
        private static bool isInsertCustomer = false;
        public static bool IsInsertCustomer
        {
            get { return isInsertCustomer; }
            set { isInsertCustomer = value; }
        }
        // bien
        public static string GetLocation()
        {
            return "";
        }
        //Phan file
        public static string AcceptFileType()
        {
            return ImageFile() + "," + FlashFile() + "," + MediaFile() + "," + ArchiveFile();
        }
        public static string ImageFile()
        {
            return ConvertType.ToString(ConfigurationManager.AppSettings["ImageFile"]).ToLower();
        }
        public static string FlashFile()
        {
            return ConvertType.ToString(ConfigurationManager.AppSettings["FlashFile"]).ToLower();
        }
        public static string MediaFile()
        {
            return ConvertType.ToString(ConfigurationManager.AppSettings["MediaFile"]).ToLower();
        }
        public static string ArchiveFile()
        {
            return ConvertType.ToString(ConfigurationManager.AppSettings["ArchiveFile"]).ToLower();
        }
        /// <summary>
        /// Kiem tra file co duoc chap nhan hay ko ?
        /// </summary>
        /// <param name="fileType">Phan mo rong cua file</param>
        /// <returns></returns>
        public static bool CheckAcceptFile(string fileType)
        {
            string type = AcceptFileType();
            if (fileType == "")
                return false;
            if (type.ToLower().Contains(fileType.ToLower()) == true)
                return true;
            return false;
        }



    }
}
